package hiar.hiscene.com.mysocket;

import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by user on 2018/5/7.
 */


public class MySocketServer {
    private MySocketListener mListener = null;
    private ServerSocketThread mServer = null;

    public void startServer(int port, MySocketListener listener) {
        mServer = new ServerSocketThread(port, listener);
        mServer.start();
    }


    public class ServerSocketThread extends Thread {
        private int mPort = 8888;
        private MySocketListener mListener = null;
        private Socket mSocket = null;

        public ServerSocketThread(int port, MySocketListener listener) {
            mPort = port;
            mListener = listener;
        }

        @Override
        public void run() {
            InputStream in = null;
            OutputStream out = null;
            ServerSocket serverSocket = null;
            try {
                serverSocket = new ServerSocket(mPort);
            } catch (IOException e) {
                Log.d("MySocket", "IOException:" + e.getMessage());
            }
            while (true) {
                try {
                    mSocket = serverSocket.accept();
                    if (mListener != null) {
                        mListener.OnConnect(true);
                    }
                    in = mSocket.getInputStream();
                    out = mSocket.getOutputStream();
                    StringBuffer sb = new StringBuffer();
                    byte buf[] = new byte[1024];
                    int len = 0;
                    while ((len = in.read(buf)) != -1) {
                        String line = new String(buf, 0, len);
                        sb.append(line);
                        if(line.endsWith(MySocket.MYSOCKET_END_FLAG) || MySocket.MYSOCKET_END_FLAG.endsWith(line)){
                            break;
                        }
                    }
                    if (mListener != null) {
                        String data = sb.toString();
                        if(data.endsWith(MySocket.MYSOCKET_END_FLAG)){
                            data = data.substring(0,data.length()-MySocket.MYSOCKET_END_FLAG.length());
                        }
                        String result = mListener.OnMessage(data);
                        if(result != null){
                            out.write(result.getBytes());
                            out.flush();
                        }
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    Log.e("MySocket", "IOException:" + e.getMessage());
                } finally {
                    try {
                        if (mSocket != null) {
                            mSocket.close();
                        }
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                    }
                    mSocket = null;
                }
            }
        }
    }
}

